var orClick = true;
var orChange = true;
var em = document.getElementById("get");
var timer;
//提示信息
function infoMsg(message,status,cb){
    var ts = document.querySelector('.TS');
    ts.style = "";
    if(status == 1){
        ts.style.background = "rgba(19,198,191,.8)";
    }
    ts.innerHTML = message;
    ts.style.opacity = 1;
    setTimeout(function () {
        ts.style.opacity = 0;
        if(cb){
            cb();
        }
    }, 2000);
}
//em内容
function emInfo(msg){
    em.style.background = '';
    em.innerHTML = msg;
}
//请求验证码
function reqYzm(phoneNum){
    $.ajax({
        url:pathName+"/api/v1/member/changePassSendSms",
        type:"POST",
        dataType:"json",
        header:{
            "Content-Type":"application/x-www-form-urlencoded;charset=UTF-8;application/json"
        },
        data:{
            "phone":phoneNum
        },
        success:function(obj) {
            //var obj = JSON.parse(data);
            console.log(obj);
            if (obj.status == 406) {
                infoMsg(obj.error);
                emInfo("获取验证码",0);
                clearInterval(timer);
            } else {
                infoMsg("已经发送验证码",1);
            }
        },
        error:function(data){
            console.log(data);
            var obj = JSON.parse(data.responseText);
            orClick = true;
            if(obj.code == 20010){
                clearInterval(timer);
                emInfo("重新发送");
                infoMsg("验证码发送失败",0);
            }else if(obj.code == 20042){
                infoMsg("该用户未注册",0);
                emInfo("重新发送");
                clearInterval(timer);
            }

        }
    })
}
//修改图形验证码
// function changeImg(phoneNum){
//     $.ajax({
//         url:pathName+"/api/v1/member/createVerifyCodeImg",
//         type:"POST",
//         data:{
//             phone:phoneNum
//         },
//         success:function(res){
//             orChange = true;
//             var main = document.getElementById("main");
//             var photoyzm = document.querySelector(".map");
//             var imgyzm = document.getElementById("imgyzm");
//             var imgCon = document.getElementById("imgCon");
//             var img = imgCon.querySelector("img");
//             main.setAttribute("class","styletwo");
//             imgyzm.style.display = "block";
//             imgCon.style.display = "block";
//             photoyzm.style.display = "block";
//             img.src = res.success.verifyCodeImg;
//         }
//     })
// }
//请求修改密码
function changePass(phoneNum,pass,phrase){
    $.ajax({
        url:pathName+"/api/v1/member/changePassword",
        type:"POST",
        dataType:"json",
        data:{
            "phone":phoneNum,
            "password":pass,
            "code":phrase
        },
        success:function(obj) {
            console.log(obj);
            if(obj.status_code == 200){
                var token = selectByKey("like_token");
                if(token){
                    deleteByKey("like_token",function(){});
                }
                infoMsg("密码修改成功",1,function(){
                    location.href = "enter.html";
                });
            }
        },
        error:function(data){
            console.log(data);
            var obj = JSON.parse(data.responseText);
            if(obj.code ==20003){
                infoMsg("验证码错误",0);
            }
        }
    })
}
window.onload = function(){
    var password = document.getElementById('passs');
    var phone = document.getElementById("phonenumber");
    var yzm = document.getElementById("yzm");
    var sub = document.getElementsByClassName("denglu")[0];
    var reg = /^1[3-9][0-9]{9}$/;
    var imgCon = document.getElementById("imgCon");
    phone.onblur = function(){
        var phoneNumber = phone.value;
        if(!reg.test(phoneNumber)){
            infoMsg("手机号码格式不正确");
        }
    };
    Tap(imgCon,function(){
        if(orChange){
            var phoneNum = phone.value;
            changeImg(phoneNum);
        }
    });
    password.onblur = function () {
        if (this.value == document.getElementById('pass').value) {
            k = 1;
            return;
        }
        k = 0;
        infoMsg("二次密码不一样");
    };
    Tap(em,function(){
        var phoneNumber = phone.value;
        if(orClick == true){
            console.log(orClick)
            orClick = false;
            if(reg.test(phoneNumber)){
                var djs = 60;
                em.style.background = "lightGray";
                em.innerHTML = "重新发送(" + djs + ")";
                timer = setInterval(function(){
                    djs -- ;
                    em.innerHTML = "重新发送(" + djs + ")";
                    if(djs <= 0){
                        clearInterval(timer);
                        orClick = true;
                        emInfo("重新发送");
                    }
                },1000);
                reqYzm(phoneNumber);
            }else{
                infoMsg("手机号码格式不正确");
            }
        }
    });
    Tap(sub,function(){
        var phoneNumber = phone.value;
        var pass1 = password.value;
        var pass2 = document.getElementById('pass').value;
        var yzmNum = yzm.value;
        if(reg.test(phoneNumber)){
            if(yzmNum){
                if(pass1 == pass2 && pass1 != ""){
                    changePass(phoneNumber,pass1,yzmNum);
                }else{
                    infoMsg("二次密码不一样");
                }
            }else{
                infoMsg("请输入验证码");
            }
        }else{
            infoMsg("手机号码格式不正确");
        }
    });

}